package com.ipcb.ieee;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * DomCatList class
 * @author Tim w.e. Lab
 *
 */
@SuppressWarnings("rawtypes")
public class DomCatList {

	//No generics

	public static List myEmpls;
	public static ArrayList<String> lName = new ArrayList<String>();
	public static ArrayList<String> lId = new ArrayList<String>(); 
	public Document dom;
	public int id = 0;
	public boolean status = true;

	/**
	 * Constructor
	 */
	public DomCatList(){
		//create a list to hold the objects
		myEmpls = new ArrayList();
	}

	/**
	 * runExample is method to run class methods 
	 */
	public void runExample() {
		//clean arrays
		lName.removeAll(lName);
		lId.removeAll(lId);
		//parse the xml file and get the dom object
		parseXmlFile();

		if(status){
			//get each element and create a object
			parseDocument();

			//Iterate through the list and print the data
			//printData();
		}
	}

	/**
	 * parseXmlFile parse xml file
	 */
	@SuppressWarnings("static-access")
	private void parseXmlFile(){
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//File file = new File(Environment.getExternalStorageDirectory()+"/catList" + id.getIdCat() +".xml"); 
			File file = new File("/data/data/com.timwelab.natta.player.android/app_nattaplayer/catList" + id +".xml");
			//parse using builder to get DOM representation of the XML file
			if(file.exists() && db.parse(file) != null){
				dom = db.parse(file);
				status = true;
			}else{
				status = false;
			}

		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
			status = false;
		}catch(SAXException se) {
			se.printStackTrace();
			status = false;
		}catch(IOException ioe) {
			ioe.printStackTrace();
			status = false;
		}

	}

	/**
	 * parseDocument parse document
	 */	
	@SuppressWarnings("unchecked")
	private void parseDocument(){
		//get the root elememt
		Element docEle = dom.getDocumentElement();

		//get a nodelist of elements
		NodeList nl = docEle.getElementsByTagName("cat");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {

				//get the element
				Element el = (Element)nl.item(i);
				//get the object
				Object e = null;
				if(getElement(el) != null){
					e = getElement(el);
				}else{
					status = false;
				}
				if(e != null){
					//add it to list
					myEmpls.add(e);
					lName.add((String) e);
					lId.add((String) e);
				}else{
					status = false;
				}
			}	
		}
	}


	/**
	 * I take an element and read the values in, create
	 * an object and return it
	 * @param empEl Element
	 * @return e GetList
	 */
	private String getElement(Element empEl) {

		//for each element get text or int values of 
		String name = Encoding(getTextValue(empEl,"text"));

		String id = getTextValue(empEl,"id");

	
		return name;
	}


	/**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content
	 * @param ele Element
	 * @param tagName String
	 * @return textVal String
	 */
	private String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue().replaceAll("myWord", "&");
		}

		return textVal;
	}


	/**
	 * Calls getTextValue and returns a int value
	 * @param ele Element
	 * @param tagName tagName
	 * @return int
	 */
	@SuppressWarnings("unused")
	private int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}

	/**
	 * Iterate through the list and print the 
	 * content to console
	 */

	/*private void printData(){

		System.out.println("No of Cat List '" + myEmpls.size() + "'.");

		Iterator it = myEmpls.iterator();
		while(it.hasNext()) {
			System.out.println(it.next().toString());
		}
	}*/

	/**
	 * Encoding xml
	 * @param s String
	 * @return aux String
	 */
	public String Encoding(String s){
		String aux = null;
		byte[] b;
		try {
			b = s.getBytes("ISO-8859-1");
			aux = new String(b);
			//Log.v("AUX",""+aux);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
		return aux;
	}

	public static List getMyEmpls() {
		return myEmpls;
	}



	public Document getDom() {
		return dom;
	}

	public void setDom(Document dom) {
		this.dom = dom;
	}

	public static ArrayList<String> getlName() {
		return lName;
	}


	public static ArrayList<String> getlId() {
		return lId;
	}



	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}


}
